from fastapi import FastAPI
from starlette.middleware.base import BaseHTTPMiddleware   # <-- 新增
from dotenv import load_dotenv
from tortoise.contrib.fastapi import register_tortoise
from . import settings
from .apps.common.views import app as common_app
from .utils import middleware

def create_app() -> FastAPI:
    app: FastAPI = FastAPI()
    load_dotenv()

    register_tortoise(
        app,
        config=settings.TORTOISE_ORM,
        generate_schemas=False,
        add_exception_handlers=True,
    )

    app.include_router(common_app, prefix='')

    # ✅ 正确注册中间件
    app.add_middleware(BaseHTTPMiddleware, dispatch=middleware.log_requests)

    return app